import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';

export default {
  name: 'qd_list_class',
  components: {},
  data () {
    return {
      siteId: '',
      studentId: '',
      spelName: '',
      area: '',
      term: '',
      weekDay: '',
      specialtyText:'专业',
      spelId: '',
      pageNo: 1,
      content: [],
      optionsArea: [],
      propsArea: {
        value: 'value',
        children: 'cities'
      },
      parentCodeArea: '0',
      optionsTerm: [],
      optionsSpelId: [],
      optionsWeekDay: [],
      selectedOptionsArea: [],
      selectedOptionsTerm: [],
      selectedOptionsSpelId: [],
      selectedOptionsWeekDay: []
    }
  },
  mounted () {
    this.siteId = window.sessionStorage.getItem('siteId')
    if (this.siteId == '71F7B4B5ADF92308E0530100007F712B') {
      this.specialtyText = '学校';
    }
    this.studentId = window.sessionStorage.getItem('studentId')
    if (this.studentId == '-1') {
      this.studentId = ''
    }
    this.classList()
  },
  methods: {
    changeAreaSelected (val) {
      var _this = this
      _this.area = val[0]
      _this.classList()
      for (var n = 0; n < _this.optionsSpelId.length; n++) {
        if (val[0] == _this.optionsSpelId[n].value) {
          _this.spelName = _this.optionsSpelId[n].label
          return
        }
      }
    },
    changeTermSelected (val) {
      var _this = this
      _this.term = val[0]
      _this.classList()
    },
    changeSpelIdSelected (val) {
      var _this = this
      _this.spelId = val[1]
      _this.classList()
    },
    changeWeekDaySelected (val) {
      var _this = this
      _this.weekDay = val[0]
      _this.classList()
    },
    lookmore () {
      var _this = this
      if (_this.pageNo >= 1) {
        _this.pageNo++
        this.classList()
      }
    },
    classList () {
      var _this = this
      //console.log(_this.siteId)
      let params = '?siteId=' + _this.siteId + '&area=' + _this.area + '&term=' + _this.term
        + '&spelId=' + _this.spelId + '&weekDay=' + _this.weekDay  + '&pageNo=' + _this.pageNo
        + '&userId=' + _this.studentId + '&userType=1';
      http.get(CONSTANT.BSPCLASSLIST + params, {
        /* siteId: _this.siteId,
         area: _this.area,
         term: _this.term,
         spelId: _this.spelId,
         weekDay: _this.weekDay,
         grade: '',
         pageNo: _this.pageNo,
         studentId: _this.studentId,
         keyWords: ''*/
      }, (res) => {
        //console.log('classList', res)
      if (typeof res.content == 'object') {
        if (res.total <= 0 || res.content.length <= 0) {
          if (_this.pageNo > 1) {
            _this.pageNo--
          }
          alert('没有更多了！')
          return
        }
        if (_this.pageNo == 1) {
          _this.content = res.content
        } else if (_this.pageNo > 1) {
          _this.content = _this.content.concat(res.content)
        }
      } else {
        _this.content = []
      }
    })
    },
    handleItemChangeArea (val) {
      var _this = this
      // 地区选择 动态获取
      if (_this.parentCodeArea != val[0]) {
        _this.parentCodeArea = val[0]
        _this.changeArea()
      }
    },
    changeArea () {
      // 区域 改成了 专业
      var _this = this
      var parms = '?siteId=' + _this.siteId;
      http.get(CONSTANT.SPECIALTYLIST + parms, {siteId: _this.siteId}, (res) => {
        //console.log('zhuanye', res)
      for (var i = 0; i < res.length; i++) {
        if (res[i].parentId == '0000') {
          var areaFirst = {}
          areaFirst.label = res[i].name
          areaFirst.value = res[i].id
          areaFirst.cities = []
          _this.optionsArea.push(areaFirst)
        }
      }
      for (var iia = 0; iia < res.length; iia++) {
        for (var iib = 0; iib < _this.optionsArea.length; iib++) {
          if (res[iia].parentId == _this.optionsArea[iib].value) {
            var areaSecond = {}
            areaSecond.label = res[iia].name
            areaSecond.value = res[iia].id
            _this.optionsArea[iib].cities.push(areaSecond)
          }
        }
      }
      // if (_this.parentCodeArea == '0') {
      //   for (var a = 0; a < res.length; a++) {
      //     var areaFirst = {}
      //     areaFirst.label = res[a].name
      //     areaFirst.value = res[a].code
      //     areaFirst.cities = []
      //     _this.optionsArea.push(areaFirst)
      //   }
      // } else {
      //   var areaSeconds = []
      //   for (var aa = 0; aa < res.length; aa++) {
      //     var areaSecond = {}
      //     areaSecond.label = res[aa].name
      //     areaSecond.value = res[aa].code
      //     areaSeconds.push(areaSecond)
      //   }
      //   for (var aab = 0; aab < _this.optionsArea.length; aab++) {
      //     if (_this.optionsArea[aab].value == _this.parentCodeArea) {
      //       _this.optionsArea[aab].cities = areaSeconds
      //       return
      //     }
      //   }
      // }
    })
    },
    termlist () {
      // 学期
      var _this = this
      var parms = '?siteId=' + _this.siteId;
      http.get(CONSTANT.TERMLIST  + parms, {siteId: _this.siteId}, (res) => {
        for (var t = 0; t < res.length; t++) {
        var termFirst = {}
        termFirst.label = res[t].resName
        termFirst.value = res[t].resCode
        _this.optionsTerm.push(termFirst)
      }
    })
    },
    arealist () {
      // 专业 改成了  区域
      var _this = this
      let params = CONSTANT.AREALIST + '?siteId=' + _this.siteId
      http.get(params, {}, (res) => {
        //console.log('quyu', res)
      _this.spelName = res[0].resName
      _this.area = res[0].resCode
      _this.selectedOptionsSpelId[0] = res[0].resCode
      _this.classList()
      for (var s = 0; s < res.length; s++) {
        var termFirst = {}
        termFirst.label = res[s].resName
        termFirst.value = res[s].resCode
        _this.optionsSpelId.push(termFirst)
      }
    })
    },
    weeklist () {
      // 时间段
      var _this = this
      var params = '?siteId=' + _this.siteId;
      http.get(CONSTANT.WEEKLIST + params, {siteId: _this.siteId}, (res) => {
        for (var w in res) {
        var weekFirst = {}
        weekFirst.label = res[w]
        weekFirst.value = w
        _this.optionsWeekDay.push(weekFirst)
        }
      })
    },
    goScheduleT (id) {
      this.$router.push({path: '/qd_list_seq',query: {classId: id}})
    },
    comeBack() {
      this.$router.push({path: '/mine'})
    }
  }
}
